import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store";
Vue.use(VueRouter);

import myOrder from "@/views/Order/myOrder/index.vue";
import groutOrder from "@/views/Order/groutOrder/index.vue";
let route = new VueRouter({
  routes: [
    {
      path: "/Home",
      component: () => import("@/views/Home/index.vue"),
    },
    {
      path: "/",
      redirect: "/Home",
    },
    {
      path: "/Login",
      component: () => import("@/views/Login/index.vue"),
    },
    {
      path: "/zhuce",
      component: () => import("@/views/zhuce/index.vue"),
    },
    {
      path: "/Search/:keyword?",
      name: "Search",
      component: () => import("@/views/Search/index.vue"),
    },
    {
      path: "/Detail/:skuId?",
      name: "Detail",
      component: () => import("@/views/Detail/index.vue"),
    },
    {
      path: "/AddCar/:skuNum?",
      name: "AddCar",
      component: () => import("@/views/AddCar/index.vue"),
    },
    {
      path: "/ShopCart",
      name: "ShopCart",
      component: () => import("@/views/ShopCart/index.vue"),
    },
    {
      path: "/Trade",
      name: "Trade",
      component: () => import("@/views/Trade/index.vue"),
      beforeEnter: (to, from, next) => {
        if (from.path == "/ShopCart") {
          next();
        } else {
          next(false);
        }
      },
    },
    {
      path: "/Order",
      name: "Order",

      component: () => import("@/views/Order/index.vue"),

      children: [
        {
          path: "myOrder",
          component: myOrder,
        },
        {
          path: "groutOrder",
          component: groutOrder,
        },
        {
          path: "/Order",
          redirect: "/Order/myOrder",
        },
      ],
    },
    {
      path: "/Pay",
      name: "Pay",
      component: () => import("@/views/Pay/index.vue"),
      beforeEnter: (to, from, next) => {
        if (from.path == "/Trade") {
          next();
        } else {
          next(false);
        }
      },
    },
    {
      path: "/PaySuccess",
      name: "PaySuccess",
      component: () => import("@/views/PaySuccess/index.vue"),
      beforeEnter: (to, from, next) => {
        if (from.path == "/Pay") {
          next();
        } else {
          next(false);
        }
      },
    },
  ],
});
route.beforeEach(async (to, from, next) => {
  let token = store.state.User.token;
  let userName = store.state.User.getRegister.name;

  //有token，已经登录
  if (token) {
    if (to.path == "/Login" || to.path == "/zhuce") {
      next("/Home");
    } else {
      if (userName) {
        next();
      } else {
        try {
          await store.dispatch("getUserInfo");

          next();
        } catch (error) {
          await store.dispatch("getLoginout");
          localStorage.removeItem("Token");
          next("/Login");
        }
      }
    }
  } else {
    //没有登录
    let toPath = to.path;
    if (
      toPath.indexOf("/Order") != -1 ||
      toPath.indexOf("/Pay") != -1 ||
      toPath.indexOf("/Trade") != -1
    ) {
      next(`/Login?redirect=` + toPath);
    } else {
      next();
    }
  }
});
export default route;
